package com.han.singleton.demo8;

import java.lang.reflect.Constructor;

/**
 * @ClassName Client
 * @Description 测试使用反射破坏单例模式
 * @Author duoduo Han
 * @Date 2024/6/3 15:59
 */
public class Client {
    public static void main(String[] args) throws Exception{
        //1. 获取Singleton的字节码对象
        Class clazz = Singleton.class;
        //2. 获取无参构造方法对象
        Constructor declaredConstructor = clazz.getDeclaredConstructor();
        //3. 取消访问检查
        declaredConstructor.setAccessible(true);
        //4. 通过反射创建对象
        Singleton instance= (Singleton) declaredConstructor.newInstance();
        Singleton instance1= (Singleton) declaredConstructor.newInstance();
        System.out.println(instance==instance1);

    }
}
